package com.ypx.ec.product.service.impl;

import com.ypx.ec.product.entity.*;
import com.ypx.ec.product.service.*;
import com.ypx.ec.product.vo.skuitemvo.SkuItemSaleAttrsVo;
import com.ypx.ec.product.vo.skuitemvo.SkuItemVo;
import com.ypx.ec.product.vo.skuitemvo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ypx.ec.common.utils.PageUtils;
import com.ypx.ec.common.utils.Query;

import com.ypx.ec.product.dao.SkuInfoDao;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    ThreadPoolExecutor poolExecutor;
    @Autowired
    SkuSaleAttrValueService saleAttrValueService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    SkuImagesService skuImagesService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo=new SkuItemVo();
        CompletableFuture<SkuInfoEntity> mainTask=CompletableFuture.supplyAsync(()->{
            SkuInfoEntity infoEntity = this.getById(skuId);
            skuItemVo.setInfo(infoEntity);
            return infoEntity;
        },poolExecutor);

        CompletableFuture<Void> getItemAttrGroupTask=mainTask.thenAcceptAsync((res)->{

            List<SpuItemAttrGroupVo> attrsBySpuId = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrsBySpuId);
        },poolExecutor);
        CompletableFuture<Void> getDescTask=mainTask.thenAcceptAsync((res)->{
            SpuInfoDescEntity descEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesp(descEntity);
        },poolExecutor);
        CompletableFuture<Void> getSkuItemSaleAttrsVoTask=mainTask.thenAcceptAsync((res)->{
            List<SkuItemSaleAttrsVo> attrs = saleAttrValueService.getAllItemSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(attrs);
        },poolExecutor);
        CompletableFuture<Void> getSkuImagesEntites=mainTask.thenAcceptAsync((res)->{
            List<SkuImagesEntity> imagesEnties=skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id",res.getSkuId()));
            skuItemVo.setImagesEntites(imagesEnties);
        },poolExecutor);
        CompletableFuture.allOf(getItemAttrGroupTask,getDescTask,getSkuItemSaleAttrsVoTask,getSkuImagesEntites).get();
        return skuItemVo;
    }

}